<?php

namespace app\api\controller;

use app\admin\model\SecondaryFavor;
use app\admin\model\SecondaryGcwAccount;
use app\admin\model\Gcw;

/**
 * 次要广场舞用户接口
 */
class Secgcwaccs extends Base
{

    public function login()
    {
        $gh_id = $this->request->post('gh_id');
        $code = $this->request->post('code');

        $openid = $this->get_openid($gh_id, $code);

        $acc = SecondaryGcwAccount::get(['openid' => $openid]);
        if (!$acc) {
            $acc = new SecondaryGcwAccount();
            $acc->openid = $openid;
            $acc->save();
        }

        $this->result($openid, $acc->id);
    }

    public function config($v)
    {
        $videoflag = true;
        $adflag = true;
        $xcxflag = true;

        if ($v == '1.1.1') {
            $videoflag = false;
            $adflag = false;
            $xcxflag = false;
        }

        $flags = array(
            'videoflag' => $videoflag,
            'adflag' => $adflag,
            'xcxflag' => $xcxflag,
        );

        $this->result('flag', $flags);
    }

    public function fetch_favor()
    {
        $favor_id = $this->request->post('collect_id');
        $account_id = $this->request->post('user_id');

        if ($f = SecondaryFavor::get([
            'account_id' => $account_id,
            'favor_id' => $favor_id
        ])) {
            $msg = 'ok';
        } else {
            $msg = 'false';
        }
        $this->result($msg);
    }

    public function add_favor()
    {
        $favor_id = $this->request->post('collect_id');
        $account_id = $this->request->post('user_id');

        $f = new SecondaryFavor();
        $f->data([
            'account_id' => $account_id,
            'favor_id' => $favor_id
        ]);
        $f->save();
        $this->success('ok');
    }

    public function del_favor()
    {
        $favor_id = $this->request->post('collect_id');
        $account_id = $this->request->post('user_id');

        $f = SecondaryFavor::get([
            'account_id' => $account_id,
            'favor_id' => $favor_id
        ]);
        if ($f) {
            $f->delete();
        }
        $this->success('ok');
    }

    public function get_favor_list()
    {
        $account_id = $this->request->post('user_id');
        $favor_list = SecondaryFavor::where('account_id', $account_id)->column('favor_id, createtime');
        $list = array();

        foreach ($favor_list as $id => $time) {
            $item = Gcw::get($id);
            $item->createtime = date('Y年m月d日', $time);
            array_push($list, $item);
        }

        $this->result('list', array_reverse($list));
    }

}
